import { defineStore } from 'pinia'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'

export default defineStore('inpatient-doctor-advice-order', {
  state: () => {
    return {
      searchState: { bqid: '', bedCode: '', zjm: '', inpAdvice: '' },
      page: { current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') ? Number(getSystemConfig('DFFAULT_PAGE_SHOW_COUNT')) : 10, total: 0 },
      loading: 0,
      tabActiveKey: 0,
      unSignData: [],
      todaySignData: [],
      allOrderData: [],
      unSignTodayCountData: {
        wqsCount: '',
        dsqCount: ''
      },
      allOrderCountData: {
        allCount: ''
      },
      allAdvType: '1',
      checkAdvType: ''
    }
  },
  actions: {
    //获取未签收数据或今日申请数据
    async fetchSignData() {
      this.loading = true
      const {
        code,
        data = [],
        msg
      } = await post('/inp/clinicalOrder/getClinicalOrderList', {
        ...this.searchState,
        type: this.tabActiveKey.toString()
      })
      this.loading = false
      if (code !== 0) return message.error(msg || '获取数据失败')
      const formatData = data.map((item) => {
        return { ...item, children: item.children?.map((i) => ({ ...i, isChid: true })) }
      })
      if (this.tabActiveKey === 0) {
        this.unSignData = formatData
      } else if (this.tabActiveKey === 1) {
        this.todaySignData = formatData
      } else {
        this.allOrderData = formatData
      }
    },
    //获取未签收,今日申请数量
    async fetchSignCountData() {
      const { code, data, msg } = await post('/inp/clinicalOrder/getClinicalOrderListCount', {
        ...this.searchState
      })
      if (code !== 0) return message.error(msg || '获取数据失败')
      this.unSignTodayCountData = data
    },
    //获取全部医嘱数据
    async fetchAllOrderData() {
      // this.page.current = current
      // this.page.pageSize = pageSize
      this.allOrderData = []
      this.loading = true
      const { code, data, msg, total } = await post('/inp/clinicalOrder/getClinicalOrderAllList', {
        ...this.searchState,
        type: this.tabActiveKey.toString(),
        currentSign: this.allAdvType === '1' ? true : false
        // pageNum: current,
        // pageSize: pageSize
      })
      this.loading = false
      if (code !== 0) return message.error(msg || '获取数据失败')
      this.page.total = total
      if (data && data.length > 0) {
        this.allOrderData = data.map((item, index) => {
          return { ...item, idx: index, children: item.children?.map((i) => ({ ...i, isChid: true })) }
        })
      } else {
        this.allOrderData = []
      }
    },
    //获取全部医嘱数量
    async fetchAllOrderCountData() {
      const { code, data, msg } = await post('/inp/clinicalOrder/getClinicalOrderAllListCount', {
        ...this.searchState,
        currentSign: this.allAdvType === '1' ? true : false
      })
      if (code !== 0) return message.error(msg || '获取数据失败')
      this.allOrderCountData = data
    }
  }
})
